import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';

import {
  Button,
  DatePicker,
  Form,
  Input,
  Modal,
  Radio,
  Tag,
  Select,
  InputNumber,
  message,
  Row,
  Col,
  Checkbox,
} from 'antd';
import { modalLayoutLg, modalLayoutMd } from '@/config/layout';
import { request } from 'umi';
import GroupUserTreeSelectCom from '@/components/GroupUserTreeSelect'
const DictModal = (props: any, refs: any) => {
  const [form] = Form.useForm();
  const ChooseUserRef = useRef();
  // const [fileList,setfileList] = useState([]);
  const [measureClassData, setmeasureClassData] = useState([]);
  const { setFieldsValue, resetFields } = form;
  const [dataSource, setDataSource] = useState([]);
  const [templateList, settemplateList] = useState([]);
  const [code, setCode] = useState('');
  const [isModalVisible, setIsModalVisible] = useState(false);
  const { TextArea } = Input;
  const messageTypeValue = Form.useWatch('message_type', form);
  const userListValue = Form.useWatch('userList', form);

  const [templateObj, settemplateObj] = useState({
    1: '',
    2: '',
    3: ''
  })
  //显示弹窗
  const showModal = () => {
    setIsModalVisible(true);
  };

  //取消
  const handleCancel = () => {
    setIsModalVisible(false);
    setCode('');
    resetFields();
    settemplateObj({
      1: '',
      2: '',
      3: ''
    })
    props.callback && props.callback();
  };
  //确定
  const handleOk = () => {
    form
      .validateFields()
      .then((vals) => {
        let Obj = form.getFieldsValue();
        Obj = {
          ...Obj,
        };

        const url = code ? '/message/group/edit' : '/message/group/add';
        if (code) {
          Obj.id = code;
        } else {
        }
        request(url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          data: Obj,
        }).then((res) => {
          message.success(res?.meta?.msg);
          setIsModalVisible(false);
          handleCancel();
        });
      })
      .catch((info) => { });
  };
  const getList = () => {
    request('/message/template/list', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: { "current": 1, "pageSize": 10000, "pageNo": 1 }
    }).then((res) => {
      settemplateList(res?.data?.data || []);
    });
  }
  const getDetail = (id: any) => {
    request('/message/group/queryById?id=' + id, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      data: {
        id,
      },
    }).then((res) => {
      setFieldsValue({
        ...res?.data,

      });
      showModal();
    });
  };
  useImperativeHandle(refs, () => ({
    init: (id: any, record: any) => {
      if (id) {
        setCode(id);
        getDetail(id);
      } else {
        resetFields();
        showModal();
      }
      getList();
      // setfileList([]);
    },
  }));
  return (
    <Modal
      title={code === '' ? '新增' : '编辑'}
      visible={isModalVisible}
      onOk={handleOk}
      onCancel={handleCancel}
      centered
      width={700}
      maskClosable={false}
      bodyStyle={{
        maxHeight: window.innerHeight - 150,
        overflowY: 'auto',
      }}
    >
      <Form form={form} {...modalLayoutMd}>
        <Form.Item
          label="消息组名称"
          name="groupName"
          rules={[{ required: true, message: '请输入消息组名称' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="选择人员"
          name="userList"
          onClick={() => ChooseUserRef.current.init(userListValue)}
          rules={[{ required: true, message: '请选择人员' }]}
        >
          <Input disabled value="" placeholder='请选择人员' />
          <div>
            {
              userListValue && userListValue.map((e, i) => {
                return <span key={i}>{i != 0 && '、'} {e.name}</span>
              })
            }
          </div>
        </Form.Item>
      </Form>
      <GroupUserTreeSelectCom
        ref={ChooseUserRef}
        onOk={(checkedUsers) => {
          form.setFieldsValue({
            userList: checkedUsers
          })
        }}
      />
    </Modal>
  );
};
const AddMCom = forwardRef(DictModal);

export default AddMCom;
